﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using log4net;
using System.Windows.Forms.DataVisualization.Charting;
using Tireguit.Nmon.Parser.NmonTables;

namespace Tireguit.Nmon.UI.Elements.Logical
{
    public class PlotSqlElements : SortedDictionary<string, SortedDictionary<string, string>>
    {
        protected ILog _log = LogManager.GetLogger(typeof(PlotSqlElements));
        public IDbConnection Connection { get; protected set; }
        protected bool quietopened = false;
        protected DateTime _startdate;
        public DateTime StartDate
        {
            get { return _startdate; }
            set { _startdate = value; }
        }
        protected DateTime _enddate;
        public DateTime EndDate
        {
            get { return _enddate; }
            set { _enddate = value; }
        }
        protected bool QuietOpen()
        {
            try
            {
                _log.Debug("start QuietOpen");
                if (Connection.State == ConnectionState.Open)
                {
                    return true;
                }
                Connection.Open();
                quietopened = true;
                return true;
            }
            catch (System.Exception ex)
            {
                _log.Error("QuietOpen", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop QuietOpen");
            }
        }
        protected bool QuietClose()
        {
            try
            {
                _log.Debug("start QuietClose");
                if (!quietopened)
                {
                    return true;
                }
                if (Connection.State == ConnectionState.Closed)
                {
                    return true;
                }
                Connection.Close();
                quietopened = false;
                return true;
            }
            catch (System.Exception ex)
            {
                _log.Error("QuietClose", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop QuietClose");
            }

        }
        public PlotSqlElements(IDbConnection conection) :
            this(conection, DateTime.MinValue, DateTime.MinValue)
        {

        }
        public PlotSqlElements(IDbConnection connection, DateTime startdate, DateTime enddate)
        {
            Connection = connection;
            _startdate = startdate;
            _enddate = enddate;
            InitInternalData();
        }
        protected void InitInternalData()
        {
            Add("Tous les CPU", InitCpuAllData());
            Add("CPU", InitCpuData());
            Add("DISK", InitDiskElements());
            Add("Taille", InitMemElements());
            Add("Usage", InitMemUsageData());
            Add("JFSFile", InitJfsFileData());
            Add("JFSINode", InitJfsInodeData());
            Add("Adapt", InitAdaptElements());
            Add("Packet", InitPacketsElements());
            Add("NetError", InitNetErrorsElements());
            Add("IOAdapt", InitIoadaptElements());
            Add("File", InitFileData());
            Add("Page", InitPageData());
            Add("Proc", InitProcData());
        }
        protected SortedDictionary<string, string> InitProcData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("Runnable", "SELECT z.[value] as X , c.[Runnable] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("Swapin", "SELECT z.[value] as X , c.[Swapin] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("pswitch", "SELECT z.[value] as X , c.[pswitch] as Y from "
                    + TablesDictionnary.Instance["PROC"] +
                    " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                    + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("syscall", "SELECT z.[value] as X , c.[syscall] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("read", "SELECT z.[value] as X , c.[read] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("write", "SELECT z.[value] as X , c.[write] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("fork", "SELECT z.[value] as X , c.[fork] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("exec", "SELECT z.[value] as X , c.[exec] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("rcvint", "SELECT z.[value] as X , c.[rcvint] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("xmtint", "SELECT z.[value] as X , c.[xmtint] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("sem", "SELECT z.[value] as X , c.[sem] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("msg", "SELECT z.[value] as X , c.[msg] as Y from "
                + TablesDictionnary.Instance["PROC"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");

            return dict;
        }
        protected SortedDictionary<string, string> InitPageData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("faults", "SELECT z.[value] as X , c.[faults] as Y from "
                + TablesDictionnary.Instance["PAGE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("pgin", "SELECT z.[value] as X , c.[pgin] as Y from "
                + TablesDictionnary.Instance["PAGE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("pgout", "SELECT z.[value] as X , c.[pgout] as Y from "
                    + TablesDictionnary.Instance["PAGE"] +
                    " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                    + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("pgsin", "SELECT z.[value] as X , c.[pgsin] as Y from "
                + TablesDictionnary.Instance["PAGE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("pgsout", "SELECT z.[value] as X , c.[pgsout] as Y from "
                + TablesDictionnary.Instance["PAGE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("reclaims", "SELECT z.[value] as X , c.[reclaims] as Y from "
                + TablesDictionnary.Instance["PAGE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("scans", "SELECT z.[value] as X , c.[scans] as Y from "
                + TablesDictionnary.Instance["PAGE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("cycles", "SELECT z.[value] as X , c.[cycles] as Y from "
                + TablesDictionnary.Instance["PAGE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");

            return dict;
        }
        protected SortedDictionary<string, string> InitFileData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("iget", "SELECT z.[value] as X , c.[iget] as Y from "
                + TablesDictionnary.Instance["FILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("namei", "SELECT z.[value] as X , c.[namei] as Y from "
                + TablesDictionnary.Instance["FILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("dirblk", "SELECT z.[value] as X , c.[dirblk] as Y from "
                    + TablesDictionnary.Instance["FILE"] +
                    " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                    + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("readch", "SELECT z.[value] as X , c.[readch] as Y from "
                + TablesDictionnary.Instance["FILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("writech", "SELECT z.[value] as X , c.[writech] as Y from "
                + TablesDictionnary.Instance["FILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("ttyrawch", "SELECT z.[value] as X , c.[ttyrawch] as Y from "
                + TablesDictionnary.Instance["FILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("ttycanch", "SELECT z.[value] as X , c.[ttycanch] as Y from "
                + TablesDictionnary.Instance["FILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("ttyoutch", "SELECT z.[value] as X , c.[ttyoutch] as Y from "
                + TablesDictionnary.Instance["FILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");

            return dict;
        }
        protected SortedDictionary<string, string> InitIoadaptElements()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("IOADAPT", "SELECT z.[value] as X , c.[value] as Y from "
                + TablesDictionnary.Instance["IOADAPT"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");

            return dict;

        }
        protected SortedDictionary<string, string> InitNetErrorsElements()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("NETERROR", "SELECT z.[value] as X , c.[value] as Y from "
                + TablesDictionnary.Instance["NETERROR"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");

            return dict;

        }
        protected SortedDictionary<string, string> InitPacketsElements()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("PACKET", "SELECT z.[value] as X , c.[value] as Y from "
                + TablesDictionnary.Instance["NETPACKET"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");

            return dict;

        }
        protected SortedDictionary<string, string> InitAdaptElements()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("NET", "SELECT z.[value] as X , c.[value] as Y from "
                + TablesDictionnary.Instance["NET"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            
            return dict;

        }
        protected SortedDictionary<string, string> InitJfsInodeData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("JFSINODE", "SELECT z.[value] as X , c.[value] as Y from "
                + TablesDictionnary.Instance["JFSINODE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            return dict;

        }
        protected SortedDictionary<string,string> InitJfsFileData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("JFSFILE", "SELECT z.[value] as X , c.[value] as Y from "
                + TablesDictionnary.Instance["JFSFILE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            return dict;
            
        }
        protected SortedDictionary<string,string> InitMemUsageData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("Numperm%", "SELECT z.[value] as X , c.[Numperm] as Y from "
                + TablesDictionnary.Instance["MEMUSE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            dict.Add("Minperm%", "SELECT z.[value] as X , c.[Minperm] as Y from "
                           + TablesDictionnary.Instance["MEMUSE"] +
                           " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                           + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            dict.Add("Maxperm%", "SELECT z.[value] as X , c.[Maxperm] as Y from "
                           + TablesDictionnary.Instance["MEMUSE"] +
                           " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                           + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            dict.Add("Minfree", "SELECT z.[value] as X , c.[Minfree] as Y from "
                           + TablesDictionnary.Instance["MEMUSE"] +
                           " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                           + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            dict.Add("Maxfree", "SELECT z.[value] as X , c.[Maxfree] as Y from "
                           + TablesDictionnary.Instance["MEMUSE"] +
                           " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                           + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            return dict;
        }
        protected SortedDictionary<string,string>InitMemElements()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("Realfree", "SELECT z.[value] as X , c.[Realfree] as Y from "
                + TablesDictionnary.Instance["MEM"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            dict.Add("Virtualfree", "SELECT z.[value] as X , c.[Virtualfree] as Y from "
                            + TablesDictionnary.Instance["MEM"] +
                            " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                            + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            dict.Add("Realtotal", "SELECT z.[value] as X , c.[Realtotal] as Y from "
                + TablesDictionnary.Instance["MEM"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            dict.Add("Virtualtotal", "SELECT z.[value] as X , c.[Virtualtotal] as Y from "
                + TablesDictionnary.Instance["MEM"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate ");
            return dict;
        }
        protected SortedDictionary<string,string> InitDiskElements()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("DISKBSIZE", "SELECT z.[value] as X , c.[value] as Y from "
                + TablesDictionnary.Instance["DISKBSIZE"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            dict.Add("DISKBUSY", "SELECT z.[value] as X , c.[value] as Y from "
               + TablesDictionnary.Instance["DISKBUSY"] +
               " c inner join " + TablesDictionnary.Instance["ZZZZ"]
               + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            dict.Add("DISKREAD", "SELECT z.[value] as X , c.[value] as Y from "
              + TablesDictionnary.Instance["DISKREAD"] +
              " c inner join " + TablesDictionnary.Instance["ZZZZ"]
              + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            dict.Add("DISKWRITE", "SELECT z.[value] as X , c.[value] as Y from "
              + TablesDictionnary.Instance["DISKWRITE"] +
              " c inner join " + TablesDictionnary.Instance["ZZZZ"]
              + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            dict.Add("DISKXFER", "SELECT z.[value] as X , c.[value] as Y from "
              + TablesDictionnary.Instance["DISKXFER"] +
              " c inner join " + TablesDictionnary.Instance["ZZZZ"]
              + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[name] = ");
            return dict;

        }
        protected SortedDictionary<string, string> InitCpuAllData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("Idle Time%", "SELECT z.[value] as X , c.[Idletime] as Y from "
                + TablesDictionnary.Instance["CPU_ALL"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("Sys Time%", "SELECT z.[value] as X , c.[Systime] as Y from "
                + TablesDictionnary.Instance["CPU_ALL"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("Wait Time%", "SELECT z.[value] as X , c.[Waittime] as Y from "
                + TablesDictionnary.Instance["CPU_ALL"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            dict.Add("User Time%", "SELECT z.[value] as X , c.[Usertime] as Y from "
                + TablesDictionnary.Instance["CPU_ALL"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate");
            return dict;
        }
        protected SortedDictionary<string, string> InitCpuData()
        {
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
            dict.Add("Idle Time%", "SELECT z.[value] as X , c.[Idletime] as Y from "
                + TablesDictionnary.Instance["CPU"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[cpu] = ");
            dict.Add("Sys Time%", "SELECT z.[value] as X , c.[Systime] as Y from "
                + TablesDictionnary.Instance["CPU"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[cpu] = ");
            dict.Add("Wait Time%", "SELECT z.[value] as X , c.[Waittime] as Y from "
                + TablesDictionnary.Instance["CPU"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[cpu] = ");
            dict.Add("User Time%", "SELECT z.[value] as X , c.[Usertime] as Y from "
                + TablesDictionnary.Instance["CPU"] +
                " c inner join " + TablesDictionnary.Instance["ZZZZ"]
                + " z on c.[zzzz] = z.[zzzz] where z.[value] between @startdate and @enddate and c.[cpu] = ");
            return dict;
        }
        protected string GetInternalReq(string name, string stat)
        {
            if (string.IsNullOrEmpty(name)&&string.IsNullOrEmpty(stat))
            {
                return string.Empty;
            }
            string req;
            
            if (name.StartsWith("CPU"))
            {
                req= this["CPU"][stat] + "'" + name + "'";
                req += " ORDER BY X";
                return req;
                
            }
            if (stat.StartsWith("DISK"))
            {
                req= this["DISK"][stat] + "'" + name + "'";
                req += " ORDER BY X";
                return req;
            }
            if (name == "JFSFile")
            {
                req = this[name]["JFSFILE"] + "'" + stat + "'";
                req += " ORDER BY X";
                return req;
            }
            if (name == "JFSINode")
            {
                req = this[name]["JFSINODE"] + "'" + stat + "'";
                req += " ORDER BY X";
                return req;
            }
            if (name == "Adapt")
            {
                req = this[name]["NET"] + "'" + stat + "'";
                req += " ORDER BY X";
                return req;
            }
            if (name == "Packet")
            {
                req = this[name]["PACKET"] + "'" + stat + "'";
                req += " ORDER BY X";
                return req;
            }
            if (name == "NetError")
            {
                req = this[name]["NETERROR"] + "'" + stat + "'";
                req += " ORDER BY X";
                return req;
            }

            if (name == "IOAdapt")
            {
                req = this[name]["IOADAPT"] + "'" + stat + "'";
                req += " ORDER BY X";
                return req;
            }

            if (!ContainsKey(name))
            {
                return string.Empty;
            }
            req= this[name][stat];
            req += " ORDER BY X";
            return req;

            throw new NotImplementedException();
        }
        protected IDbCommand GetInternalCommand(string textcaommand)
        {
            if (string.IsNullOrEmpty(textcaommand))
            {
                return null;
            }
            IDbCommand command = Connection.CreateCommand();
            command.CommandText = textcaommand;
            IDataParameter paramstart = command.CreateParameter();
            paramstart.ParameterName = "@startdate";
            paramstart.DbType = DbType.DateTime;
            paramstart.Value = StartDate;
            command.Parameters.Add(paramstart);
            paramstart = command.CreateParameter();
            paramstart.ParameterName = "@enddate";
            paramstart.DbType = DbType.DateTime;
            paramstart.Value = EndDate;
            command.Parameters.Add(paramstart);
            return command;
        }
        public DataTable GetDataView(string name)
        {
            IDbCommand command = null;
            IDataReader reader = null;
            try
            {
                _log.Debug("start GetDataView");
                if (!(name.StartsWith("S+") && name.Contains(':')))
                {
                    _log.Error(name + " Is Not Statical Element");
                    return null;
                }
                if (!QuietOpen())
                {
                    _log.Error("Can't Open Database");
                    return null;
                }
                string cleanedname = name.Remove(0, 2);
                char[] sep = { ':' };
                string[] splitedname = cleanedname.Split(sep);
                command = GetInternalCommand(GetInternalReq(splitedname[0], splitedname[1]));
                if (command == null)
                {
                    return null;
                }
                reader = command.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(reader);
                dt.AcceptChanges();
                return dt;


            }
            catch (System.Exception ex)
            {
                _log.Error("GetDataView", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception)
                {

                }

                _log.Debug("start GetDataView");
            }
        }
        public bool GetSerie(Series serie,string name, string fieldX, string fieldY)
        {
            try
            {
                _log.Debug("start GetSerie");
                DataTable dt = GetDataView(name);
                if (dt == null)
                {
                    return false;
                }
                serie.Points.DataBindXY(dt.DefaultView, fieldX, dt.DefaultView, fieldY);
                return true;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetSerie", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop GetSerie");
            }
        }
        public bool GetSerie(Series serie,string name)
        {
            return GetSerie(serie,name, "X", "Y");
        }
        public bool GetSerie(Series serie)
        {
            return GetSerie(serie, serie.Name, "X", "Y");
        }
    }
}
